package TUILayer;//makes this class a part of the TUILayer package
import java.util.*;
import ControlLayer.AddressCtr; //The TUILayer needs to know the address controller in the ControlLayer
import ModelLayer.Person; //The TUILayer needs to know class from the Modellayer to create an instance of Person

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private static AddressCtr addrCtr; //reference to the ControlLayer  
    
   /**
     * Constructor for objects of class AddressBookUI
     */
    public AddressBookUI()
    {
       System.out.print("\f");
       addrCtr = addrCtr.getInstance();
 
    }
   
    /**
     * initiating method for the address menu.
     */
    public void start()
    {
       addressMenu();
    }
    
    /**
     *  Returns the menu, from where the user can make a choise.
     */
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeAddressBookMenu(); 
          if(choise == 1){   //show person
              showInfoAboutPerson();
          }
          
          if(choise == 2){
              createPerson();
          }

          if(choise == 3){
              deletePerson();
          }  
          
          if(choise == 4){
              updatePerson();
          }

          if(choise == 5){
              listAllPersons();
          }
          
          if(choise == 6){
              System.out.print("\f");
              exit=true;              
          }
          
          if(choise > 6){
              printValidError();
          }

        }
    }
    
    /**
     * prints range of choices and takes an input from user.
     */
    public int writeAddressBookMenu()
    {      
            int choise = 0;
            Scanner keyboard = new Scanner(System.in);// makes an object keyboard to read input from the screen
            System.out.println("*** AddressBook Menu ***"); 
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            try
            {
                choise = keyboard.nextInt();
            }            
            catch(Exception e)
            {
                printNumError();             
            }
            return choise;
    }
   
    /**
     * takes an input of id and searches address container for a match. Cancel option by typing 0 (zero)
     */
    public long getId()
    {         
        long i = inputPersonsID();
        boolean exit = false;
        
        while(addrCtr.getPerson(i) == null && !exit)
        {
            System.out.println(" \f id not found, try again. Type 0 (zero) to exit.");
            i = inputPersonsID();
            if (i == 0)
                {
                exit = true;
                System.out.print("\f");
                }
        }
        return i;
    }
   
    /**
     * takes a scanner input of id and verifies it as type long before passing it on.
     */
    public long inputPersonsID()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Id: ");
        long id = 0;
        try{
            id = keyboard.nextLong();
        }
        catch (InputMismatchException e) {
            System.out.println("\f");
        }     
        return id;
    }
    
    /**
     * Methods for capturing information to pass on 
     */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    /**
     * Methods for capturing information to pass on 
     */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * Methods for capturing information to pass on 
     */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    
    /**
     * Methods for capturing information to pass on 
     */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    /**
     * Methods for capturing information to pass on 
     */
    public String inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    /**
     * Methods for capturing information to pass on 
     */
    public int inputInt()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write number: ");
        int i = 0;
        try{
            i = keyboard.nextInt();
        }
        catch (InputMismatchException e) {
            System.out.println("\f");
        }     
        return i;
    }
       
    /**
     * creates a new instance of Person and adds it to a containerclass
     */
    public void createPerson()
    {
        System.out.println("\f You have chosen to create a new person");
            String name = inputName();
            String address = inputAddress();
            String postalCode = inputPostalCode();
            String city = inputCity();
            String phone = inputPhone();
            
            addrCtr.createPerson(name, address, postalCode, city, phone);
        System.out.print("\f");
    }
    
    /**
     * Returns an object of type Person
     */
    private Person getPerson(long id)
    {
        return addrCtr.getPerson(id);
    }
    
    /**
     * takes an input of type id, checks it for validity and prints information about a person if found.
     */
    public void showInfoAboutPerson()
    {
        System.out.println("\f You have chosen to show the information about a person.");
        long i = getId();
        if(i != 0)
        {
            System.out.println("\f*****************************");
            System.out.println("ID: " + getPerson(i).getId() );
            System.out.println("Name: " + getPerson(i).getName() );
            System.out.println("Address: " + getPerson(i).getAddress() );
            System.out.println("Postal code: " + getPerson(i).getPostalCode() );
            System.out.println("City: " + getPerson(i).getPostalCode() );
            System.out.println("Phone: " + getPerson(i).getPhone() );
            System.out.println("*****************************");
            System.out.println("\n");
        }
    }
    
    /**
     *  takes an input of type id, checks it for validity and removes a person if found.
     */
    private void deletePerson()
    {
        System.out.println("\f You have chosen to delete a person");  
        long id = getId();
        if(id != 0) 
        {
            addrCtr.deletePerson(id);
            System.out.println("\f The person is deleted.\n");
        }
    }
    
    /**
     * takes an input of type id, checks it for validity and returns a menu for updating a person if found.
     */
    private void updatePerson()
    {
        System.out.println(" \f You have chosen to update a person");
        long i = getId();
        int[] allowedAnswers = {1,2,3,4,5,6};
        boolean exit = false;
        if(i == 0){
        exit = true;
        }
        
        while (!exit) 
        {
        System.out.print("\f");
        
        getPerson(i).printPerson();
        
        System.out.println("What do you want to change?");
        System.out.println(" (1) name");
        System.out.println(" (2) address");
        System.out.println(" (3) postal code");
        System.out.println(" (4) city");
        System.out.println(" (5) phone");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        System.out.println("\n");
            
            int answer = inputInt();                                        
                if (answer == 1){  
                     String name = inputName();
                     addrCtr.getPerson(i).setName(name);
                }
                    
                else if (answer == 2) {
                     String address = inputAddress();
                     addrCtr.getPerson(i).setAddress(address);
                }
                    
                else if (answer == 3) {
                     String postalCode = inputPostalCode();
                     addrCtr.getPerson(i).setPostalCode(postalCode);
                }
               
                else if (answer == 4) {
                    String city = inputCity();
                    addrCtr.getPerson(i).setCity(city);
                }   
               
                else if (answer == 5) {
                    String phone = inputPhone();
                    addrCtr.getPerson(i).setPhone(phone);
                }
                
                else if (answer == 6) {
                    exit = true;
                    System.out.print("\f");
                }
                
                else  if(!allowedAnswers.equals(answer))
                    
                    printNumError();
                }         
        }
    
   /**
    * prints a list of all persons registered in the system
    */
    public void listAllPersons()
    {
        System.out.println("\fList of all persons: ");
        System.out.println("----------------------");
        addrCtr.listAllPersons();
        System.out.println("----------------------");
        System.out.print("\n");
    }
    
    /**
     * returns an error message in case of invalidt input
     */
    private void printNumError()
    {
        System.out.println("\f Invalid input, it has to be a number. \n");
    }
    
     /**
     * returns an error message in case of invalidt input
     */
    private void printValidError()
    {
        System.out.println("\f Invalid indput, out of range. \n");
    }
}
            
        
